//author Dreena Biswas

package model;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import au.com.bytecode.opencsv.*;

public class Model {
	
	//ADD
	public void addContact(Contact obj){
		System.gc();
		String csv = "files/output.csv";
		CSVWriter writer;
		
		try {			
			writer = new CSVWriter(new FileWriter(csv,true));
			String [] contact = {obj.get_fName(), 
									obj.get_lName(), 
									obj.get_group(), 
									obj.get_homeAdd(), 
									obj.get_compAdd(), 
									obj.get_personalMobile(), 
									obj.get_personalLand(), 
									obj.get_personalMobile(), 
									obj.get_personalLand(), 
									obj.get_homePage(), 
									obj.get_emailId(), 
									obj.get_photo(), 
									obj.get_date()};
			 
			writer.writeNext(contact);
			writer.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		 
		
	}// end of ADD
	

	// DELETE
	public void deleteContact(Contact cont) throws IOException{
		System.gc();
		String csvFilename = "files/output.csv";
		String csvFiletemp = "files/output_temp.csv";
		CSVWriter writer;
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		writer = new CSVWriter(new FileWriter(csvFiletemp));
		while((row = csvReader.readNext()) != null) {
			//System.out.println(row[0]);
			if(!(row[0].equals(cont.get_fName()) && row[1].equals(cont.get_lName()))){
				String [] contact = {row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12]};
				
				System.out.println(contact[0]);
				writer.writeNext(contact);
			}
			
		}
			writer.close();
			csvReader.close();
			File file1 = new File("files/output.csv");
			file1.delete();
			File file2 = new File("files/output_temp.csv");
			file2.renameTo(file1);						
	} // end of DELETE
	
	
	// DISPLAY
	public ArrayList<String> displayContact(String fName, String lName) throws IOException{
		String csvFilename = "files/output.csv";
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		ArrayList<String> cont = new ArrayList<String>();
		
		while((row = csvReader.readNext()) != null) {
			if(row[0].equals(fName) && row[1].equals(lName))
			{
				cont.add(new StringBuilder().append("First Name :     ").append(row[0]).toString());
				cont.add(new StringBuilder().append("Last Name :     ").append(row[1]).toString());
				cont.add(new StringBuilder().append("Group :     ").append(row[2]).toString());
				cont.add(new StringBuilder().append("Home Address :     ").append(row[3]).toString());
				cont.add(new StringBuilder().append("Company Address :     ").append(row[4]).toString());
				cont.add(new StringBuilder().append("Personal Mobile :     ").append(row[5]).toString());
				cont.add(new StringBuilder().append("Personal Land :     ").append(row[6]).toString());
				cont.add(new StringBuilder().append("Professional Mobile :     ").append(row[7]).toString());
				cont.add(new StringBuilder().append("Professional Land :     ").append(row[8]).toString());
				cont.add(new StringBuilder().append("Home Page Link :     ").append(row[9]).toString());
				cont.add(new StringBuilder().append("Email id :     ").append(row[10]).toString());
				cont.add(new StringBuilder().append("Photo :     ").append(row[11]).toString());
				cont.add(new StringBuilder().append("Date :     ").append(row[12]).toString());
			}	
			}
		//...
		csvReader.close();
		return cont;
	} // end of DISPLAY
	

	public static ArrayList<String> displayAllContact() throws IOException{
		System.gc();
		String csvFilename = "files/output.csv";
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		ArrayList<String> cont = new ArrayList<String>();
		
		
		while((row = csvReader.readNext()) != null) {
			
				cont.add(new StringBuilder().append(row[0]).append(' ').append(row[1]).toString());
				

			
			}
		//...
		csvReader.close();
		Collections.sort(cont, String.CASE_INSENSITIVE_ORDER);
		return cont;
	} // end of DISPLAY
	
	public static ArrayList<Contact> getAllContactObjects() throws IOException{
		System.gc();
		String csvFilename = "files/output.csv";
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		ArrayList<Contact> cont = new ArrayList<Contact>();
		
		
		while((row = csvReader.readNext()) != null) {
				Contact obj = new Contact();
				obj.set_fName(row[0]);
				obj.set_lName(row[1]);
				obj.set_group(row[2]);
				obj.set_homeAdd(row[3]);
				obj.set_compAdd(row[4]);
				obj.set_personalMobile(row[5]);
				obj.set_personalLand(row[6]);
				obj.set_profMobile(row[7]);
				obj.set_profLand(row[8]);
				obj.set_homePage(row[9]);
				obj.set_emailId(row[10]);
				obj.set_photo(row[11]);
				obj.set_date(row[12]);
				
				cont.add(obj);
				//cont.add(new StringBuilder().append(row[0]).append(' ').append(row[1]).toString());
			
			}
		//...
		System.gc();
		csvReader.close();
		Collections.sort(cont, new Comparator<Contact>() {
			  public int compare(Contact o1, Contact o2) {
			      try {
					if (new SimpleDateFormat("dd/MM/YYYY", Locale.ENGLISH).parse(o1.get_date()) == null || new SimpleDateFormat("dd/MM/YYYY", Locale.ENGLISH).parse(o2.get_date()) == null)
					    return 0;
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			      return o1.get_date().compareTo(o2.get_date());
			  }
			});
		return cont;
	} // end of DISPLAY
	
	
	
	//SORT ALPHA
	public void sortAlpha(){
			
	} //end of SORT ALPHA
	
	
	// SORT GROUP
	public ArrayList<String> sortGroup(String group) throws IOException{
		String csvFilename = "files/output.csv";
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		ArrayList<String> contarr = new ArrayList<String>();
		while((row = csvReader.readNext()) != null) {
			if(row[2].equals(group))
			{
				String ex = (row[0]
			              + " " + row[1]);
			contarr.add(ex);}
		}
		
		//...
		csvReader.close();
		return(contarr);
	} // end of SORT GROUP
	
	
	//SORT DATE
	public void sortDate(ArrayList<String> contacts){
		for(String s : contacts){
			System.out.println(s);
			
		}
		/*Collections.sort(myList, new Comparator<contacts>() {
			  public int compare(MyObject o1, MyObject o2) {
			      if (o1.getDateTime() == null || o2.getDateTime() == null)
			        return 0;
			      return o1.getDateTime().compareTo(o2.getDateTime());
			  }
			});*/
		
	} //end of SORT DATE

	
	//EDIT CONTACT
	
	public Contact editContact1(Contact cont) throws IOException {
		String csvFilename = "files/output.csv";
		String csvFiletemp = "files/output_temp.csv";
		CSVWriter writer;
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		writer = new CSVWriter(new FileWriter(csvFiletemp));
		Contact cont_to_edit = new Contact();
		while((row = csvReader.readNext()) != null) {
			//System.out.println(row[0]);
			if(!(row[0].equals(cont.get_fName()) && row[1].equals(cont.get_lName()))){
				System.out.println("Searching");
					
				
			}
			else{
				 
				cont_to_edit.set_fName(row[0]);
				cont_to_edit.set_lName(row[1]);
				cont_to_edit.set_group(row[2]);
				cont_to_edit.set_homeAdd(row[3]);
				cont_to_edit.set_compAdd(row[4]);
				cont_to_edit.set_personalMobile(row[5]);
				cont_to_edit.set_personalLand(row[6]);
				cont_to_edit.set_profMobile(row[7]);
				cont_to_edit.set_profLand(row[8]);
				cont_to_edit.set_homePage(row[9]);
				cont_to_edit.set_emailId(row[10]);
				cont_to_edit.set_photo(row[11]);
				cont_to_edit.set_date(row[12]);
				
				return cont_to_edit;
				
				//System.out.println(contact[0]);
				//writer.writeNext(contact);
				
			}
			
		}
			writer.close();
			csvReader.close();

			return cont_to_edit;
		
	} // end of EDIT 
 

	//Edit Write
	public static void editContact2(Contact cont) throws IOException {
		
		//System.out.println(fName+lName+group+homeAdd+compAdd+personalMobile+personalLand+profMobile+profLand+homePage+emailId+photo+date);
		System.gc();
		String csvFilename = "files/output.csv";
		String csvFiletemp = "files/output_temp.csv";
		
		CSVWriter writer;
		
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		writer = new CSVWriter(new FileWriter(csvFiletemp));
		
		String[] row = null;
		
		while((row = csvReader.readNext()) != null) {
			//System.out.println(row[0]);
			if(!(row[0].equals(cont.get_fName()) && row[1].equals(cont.get_lName()))){
				String [] contact = {row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12]};
				writer.writeNext(contact);
			}
			else{
				String [] contact = {cont.get_fName(), 
									 cont.get_lName(), 
									 cont.get_group(), 
									 cont.get_homeAdd(), 
									 cont.get_compAdd(), 
									 cont.get_personalMobile(), 
									 cont.get_personalLand(), 
									 cont.get_profMobile(), 
									 cont.get_profLand(), 
									 cont.get_homePage(), 
									 cont.get_emailId(), 
									 cont.get_photo(), 
									 cont.get_date()};
				writer.writeNext(contact);

				
			}
			
		}
		csvReader.close();
		writer.close();
		

		File file = new File("files/output_temp.csv");
		
		File file2 = new File("files/output.csv");
		

		
		if(file2.exists()) {System.out.println(file2.delete());}

		    // Rename file (or directory)
		boolean success = file.renameTo(file2);
		if (!success) {
		       System.out.println("File was not successfully renamed");
		       
		    }
			
			
			
			
			
		
	} // Sort by Name
	
	public static ArrayList<String> sortbyName() throws IOException{
		String csvFilename = "files/output.csv";
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		ArrayList<String> cont = new ArrayList<String>();
		
		while((row = csvReader.readNext()) != null) {
			
				cont.add(new StringBuilder().append(row[0]).append(' ').append(row[1]).toString());
				//System.out.print(new StringBuilder().append(row[0]).append(' ').append(row[1]).append("  ").toString());

			
			}
		//...
		csvReader.close();
		return cont;
	} // end of DISPLAY
		

}
